package com.dmw.controller;

import com.dmw.dto.Dto;
import com.dmw.entity.DmOrder;
import com.dmw.service.DmOrderService;
import com.dmw.util.DtoUtil;
import com.dmw.util.RedisUtil;
import com.dmw.vo.OrderNoVO;
import com.dmw.vo.orderVO.OrderInfoInVo;
import com.dmw.vo.orderVO.OrderInfoOutVo;
import com.dmw.vo.placeOrderVO.AddOrderVO;
import com.dmw.vo.placeOrderVO.OrderOfOrderNoVO;
import com.dmw.vo.placeOrderVO.OrderPaymentStateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ProviderOrderController {

    @Autowired
    private DmOrderService dmOrderService;
    @Autowired
    private RedisUtil redisUtil;

    /**
      * 根据订单状态、订单创建时间、关键词(商品名称，订单号，商品编号模糊)
     *  from 订单管理页面API 1
      * @param map
      * @return
      */
    @PostMapping("/queryOrderList")
    public Dto queryOrderList(@RequestBody Map<String,Object> map){

        System.out.println("come");
        System.out.println(map);
        List<OrderInfoOutVo> list=null;
        try {
            list = dmOrderService.queryOrderList(map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return DtoUtil.returnSuccess("查询成功！", list);
    }

    /**
     * 下单接口
     * @param vo
     * @return
     */
    @PostMapping("/createOrder")
    public Dto createOrder(@RequestBody AddOrderVO vo){
        System.out.println("下单接口");
        System.out.println(vo);

        //token 失效处理
        String token = vo.getToken();
        String tokenKey="token:"+token;
        if (token == null||!redisUtil.hasKey(tokenKey)) {
            return DtoUtil.returnFail("token失效，请重登录", "100000");
        }

        String orderNo =null;
        try {
            orderNo = dmOrderService.createOrder(vo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> map=new HashMap<>();
        map.put("orderNo",orderNo);
        OrderNoVO o=new OrderNoVO();
        o.setOrderNo(orderNo);
        return DtoUtil.returnDataSuccess(map);
    }

    /**
     * 根据订单号查询订单
     * @param map
     * @return
     */
    @PostMapping("/queryOrderByOrderNo")
    public Dto queryOrderByOrderNo(@RequestBody Map<String, Object> map){
        System.out.println("come");
        System.out.println(map.get("orderNo"));

        List<OrderOfOrderNoVO> list=null;
        try {
           list = dmOrderService.queryOrderByOrderNo(map);
           for(int i=0;i<list.size();i++){
               if(i!=0) {
                   list.get(0).setSeatNames(list.get(0).getSeatNames().concat(","+list.get(i).getSeatNames()));
               }
           }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return DtoUtil.returnDataSuccess(list.get(0));
    }

    /**
     *监听订单支付状态接口
     * @param map
     * @return
     */
    @PostMapping("/queryOrderState")
    public Dto queryOrderState(@RequestBody Map<String,Object> map){
        System.out.println(">>>监听订单支付状态接口");
        System.out.println(map.get("orderNo"));
        OrderPaymentStateVO vo=null;
        try {
            vo = dmOrderService.queryOrderState(String.valueOf(map.get("orderNo")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(vo);
    }

}
